package com.leetcode.study;

/**
 * 485. 最大连续 1 的个数
 * https://leetcode.cn/problems/max-consecutive-ones/
 */
public class L485 {

    public static void main(String[] args) {

        int[] array = {1, 1, 0, 1, 1, 1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,2};

        System.out.println(findMaxConsecutiveOnes(array));
        System.out.println(findMaxConsecutiveOnesStand(array));

    }

    public static int findMaxConsecutiveOnes(int[] nums) {

        int temp = 0;
        int res = 0;

        for (int i : nums) {
            if (i == 1) {
                temp++;
            }

            if (i != 1) {
                res = Math.max(temp, res);
                temp = 0;
            }
        }
        res = Math.max(temp, res);
        return res;
    }

    /**
     * 题解
     * @param nums
     * @return
     */
    public static int findMaxConsecutiveOnesStand(int[] nums) {
        int maxCount = 0, count = 0;
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if (nums[i] == 1) {
                count++;
            } else {
                maxCount = Math.max(maxCount, count);
                count = 0;
            }
        }
        maxCount = Math.max(maxCount, count);
        return maxCount;
    }
}
